package com.sheep.spark.job;

import com.sheep.spark.util.ConfSupport;
import com.sheep.spark.util.SparkSupport;
import org.apache.log4j.Logger;
import org.apache.spark.api.java.StorageLevels;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.streaming.api.java.JavaDStream;
import org.apache.spark.streaming.api.java.JavaReceiverInputDStream;

import java.io.Serializable;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 16-6-2
 * Time: 上午10:29
 * To change this template use File | Settings | File Templates.
 */
abstract public class NetworkStreamJob implements Serializable {
    private static final Logger logger = Logger.getLogger(NetworkStreamJob.class);

    public NetworkStreamJob() {
        try {
            init();
        } catch (Exception e) {
            logger.error(e);
        }
    }

    private void init() throws Exception {
        run(SparkSupport.getJavaStreamingContext().socketTextStream(
                ConfSupport.getStreamNetworkIP(), ConfSupport.getStreamNetworkPort(), StorageLevels.MEMORY_AND_DISK_SER));
        SparkSupport.getJavaStreamingContext().start();
        SparkSupport.getJavaStreamingContext().awaitTermination();
    }

    abstract public void run(JavaReceiverInputDStream<String> lines);
}

